package com.da.lon.wang.xlg.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.adapter.CountrySiftLeftAdapter;
import com.da.lon.wang.xlg.adapter.CountrySiftRightAdapter;
import com.da.lon.wang.xlg.bean.CountrySiftResultBean;
import com.da.lon.wang.xlg.bean.CountrySiftResultBean.CountrySiftBean;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.http.HttpLoader;
import com.da.lon.wang.xlg.http.HttpLoader.OnWebLoadListener;
import com.da.lon.wang.xlg.util.DialogUtil;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.da.lon.wang.xlg.util.XLGDataUtils;
import com.umeng.analytics.MobclickAgent;

import net.tsz.afinal.http.AjaxParams;

import java.util.ArrayList;

public class CountrySiftingActivity extends Activity implements
        OnItemClickListener, OnClickListener {
    ExpandableListView left;
    ListView right;
    DialogUtil dialogUtil;
    String cid;
    ArrayList<CountrySiftBean> datas;
    ImageButton ouka_home_buyer_left;
    TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_country_sifting);
        dialogUtil = new DialogUtil(this);
        left = (ExpandableListView) findViewById(R.id.country_sift_left);
        right = (ListView) findViewById(R.id.country_sift_right);
        ouka_home_buyer_left = (ImageButton) findViewById(R.id.ouka_home_buyer_left);
        ouka_home_buyer_left.setOnClickListener(this);
        title = (TextView) findViewById(R.id.title);
        right.setOnItemClickListener(this);
        left.setOnGroupClickListener(new OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                                        int groupPosition, long id) {
                return true;
            }
        });
        left.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                PreferenceUtils.getInstance().SetSettingString(
                        PreferencesConfig.SEARCH_SATTR,
                        datas.get(groupPosition).getValue().get(childPosition)
                                .getName());
                Intent intent = new Intent(CountrySiftingActivity.this,
                        ResultSearchActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("cid", cid);
                intent.putExtras(bundle);
                startActivity(intent);
                return true;
            }
        });
        getData();
    }

    private void getData() {

        Intent intent = getIntent();
        cid = intent.getStringExtra("cid");
        String name = intent.getStringExtra("name");
        title.setText(name);
        HttpLoader<CountrySiftResultBean> loader = new HttpLoader<CountrySiftResultBean>(
                this, XLGConstants.URL + "Product-allbrand",
                CountrySiftResultBean.class);
        AjaxParams params = new AjaxParams();
        params.put("cid", cid);
        params.put("token", XLGDataUtils.encrypt2MD5("Product" + "2015#$%"
                + "allbrand"));
        loader.setManagerListener(new OnWebLoadListener<CountrySiftResultBean>() {

            @Override
            public void OnStart() {
                dialogUtil.showDialog();
            }

            @Override
            public void OnError(String error) {
                dialogUtil.dismissDialog();
            }

            @Override
            public void onSuccess(CountrySiftResultBean t) {
                dialogUtil.dismissDialog();
                if (null != t && "000000".equals(t.getCood())) {
                    datas = t.getResult().getBrand_result();
                    CountrySiftLeftAdapter leftAdapter = new CountrySiftLeftAdapter(
                            datas, CountrySiftingActivity.this);
                    left.setAdapter(leftAdapter);
                    // 默认展开
                    for (int i = 0; leftAdapter != null
                            && i < leftAdapter.getGroupCount(); i++) {
                        left.expandGroup(i);
                    }
                    CountrySiftRightAdapter rightAdapter = new CountrySiftRightAdapter(
                            datas, CountrySiftingActivity.this);
                    right.setAdapter(rightAdapter);
                }
            }
        });
        loader.loadData(params);


    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        left.setSelectedGroup(position);
    }

    @Override
    public void onClick(View v) {
        finish();
    }
}
